package cn.edu.ncu.housinghub.mapper;

import cn.edu.ncu.housinghub.dto.request.contract.ContractVerifyRequest;
import cn.edu.ncu.housinghub.entity.contract.Contract;
import cn.edu.ncu.housinghub.entity.contract.ContractApplication;
import cn.edu.ncu.housinghub.entity.contract.ContractContentInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ContractMapper {
    /**
     * 租客像向房东发起签约申请,插入合同初始信息
     * @param application 签约信息
     * @return 插入的记录数
     */
    @Insert("INSERT INTO contractapplication (user_id,landlord_id,house_id, real_name,id_card,phone,deposit,lease_start_date,lease_end_date,monthly_rent,payment_method,apply_time,status) " +
            "VALUES (#{userId}, #{landlordId}, #{houseId}, #{realName},#{idCard}, #{phone}, #{deposit},#{leaseStartDate},#{leaseEndDate},#{monthlyRent},#{paymentMethod},#{applyTime},#{status})")
    void insertInitContract(ContractApplication application);

    /**
     * 房东查看签约申请列表
     * @param landlordId 房东ID
     * @return 签约申请列表
     */
    @Select("SELECT ca.id as contractId, ca.user_id as `tenant.tenantId`, ca.house_id as `house.houseId`, " +
            "ca.landlord_id as `landlord.landlordId`, ca.real_name, ca.id_card, ca.phone, " +
            "ca.lease_start_date as startDate, ca.lease_end_date as endDate, " +
            "ca.monthly_rent as monthRent, ca.deposit, ca.payment_method as paymentType, " +
            "ca.status as signStatus, ca.apply_time as applyTime " +
            "FROM contractapplication ca WHERE ca.landlord_id = #{landlordId} AND ca.status = 0")
    List<Contract> selectContractApplicationsByLandlordId(Long landlordId);

    /**
     * 房东同意/拒绝合同申请
     * @param contractVerifyRequest 合同验证请求参数
     */
    @Update("<script>" +
            "UPDATE contractapplication " +
            "SET status = #{signStatus}" +
            "<if test='signStatus == 2'>" +
                "<if test='refuseReason != null and refuseReason != \"\"'>" +
                ", refuse_reason = #{refuseReason}" +
                "</if>" +
            "</if>" +
            "WHERE id = #{contractId}" +
            "</script>")
    void updateContractStatus(ContractVerifyRequest contractVerifyRequest);

    /**
     * 插入合同内容信息
     * @param contractContentInfo 合同内容信息
     * @return 插入的记录数
     */
    int insertContractContentInfo(ContractContentInfo contractContentInfo);

    /**
     * 根据合同申请ID查询合同申请详情
     * @param contractId 合同申请ID
     * @return 合同申请详情
     */
    @Select("SELECT ca.id as contractId, ca.user_id as `tenant.tenantId`, ca.house_id as `house.houseId`, " +
            "ca.landlord_id as `landlord.landlordId`, ca.real_name, ca.id_card, ca.phone, " +
            "ca.lease_start_date as startDate, ca.lease_end_date as endDate, " +
            "ca.monthly_rent as monthRent, ca.deposit, ca.payment_method as paymentType, " +
            "ca.status as signStatus, ca.refuse_reason as refuseReason, ca.apply_time as applyTime " +
            "FROM contractapplication ca WHERE ca.id = #{contractId}")
    Contract selectContractById(@Param("contractId") Long contractId);

    /**
     * 更新合同内容信息
     * @param contractContentInfo 合同内容信息
     * @return 更新的记录数
     */
    int updateContractContentInfo(ContractContentInfo contractContentInfo);

    /**
     * 保存合同模板
     * @param contractContentInfo 合同内容信息
     * @return 插入的记录数
     */
    int saveContractTemplate(ContractContentInfo contractContentInfo);

    /**
     * 更新合同PDF路径
     * @param contractId 合同ID
     * @param contractUrl PDF路径
     * @return 更新的记录数
     */
    int updateContractUrl(@Param("contractId") Long contractId, @Param("contractUrl") String contractUrl);

    /**
     * 租客查看自己的签约申请列表
     * @param tenantId 租客ID
     * @return 签约申请列表
     */
    @Select("SELECT ca.id as contractId, ca.user_id as `tenant.tenantId`, ca.house_id as `house.houseId`, " +
            "ca.landlord_id as `landlord.landlordId`, ca.real_name as realName, ca.id_card as idCard, ca.phone, " +
            "ca.lease_start_date as startDate, ca.lease_end_date as endDate, " +
            "ca.monthly_rent as monthRent, ca.deposit, ca.payment_method as paymentType, " +
            "ca.status as signStatus, ca.refuse_reason as refuseReason, ca.apply_time as applyTime " +
            "FROM contractapplication ca WHERE ca.user_id = #{tenantId} ORDER BY ca.apply_time DESC")
    List<Contract> selectContractApplicationsByTenantId(Long tenantId);
}
